using System.Collections.Generic;

namespace UnityEngine.Recorder
{
	public class SceneHook
	{
		private const string k_HostGoName = "UnityEngine-Recorder";

		internal static GameObject GetGameObject(bool createIfAbsent)
		{
			GameObject gameObject = GameObject.Find("UnityEngine-Recorder");
			if (gameObject == null && createIfAbsent)
			{
				gameObject = new GameObject("UnityEngine-Recorder");
				if (!Verbose.enabled)
				{
					gameObject.hideFlags = HideFlags.HideInHierarchy;
				}
			}
			return gameObject;
		}

		private static GameObject GetRecordingSessionsRoot(bool createIfAbsent)
		{
			GameObject gameObject = GetGameObject(createIfAbsent);
			if (gameObject == null)
			{
				return null;
			}
			Transform transform = gameObject.transform.Find("RecordingSessions");
			GameObject gameObject2;
			if (transform == null)
			{
				gameObject2 = new GameObject("RecordingSessions");
				gameObject2.transform.parent = gameObject.transform;
			}
			else
			{
				gameObject2 = transform.gameObject;
			}
			return gameObject2;
		}

		public static GameObject GetSettingsRoot(bool createIfAbsent)
		{
			GameObject gameObject = GetGameObject(createIfAbsent);
			if (gameObject == null)
			{
				return null;
			}
			Transform transform = gameObject.transform.Find("Settings");
			GameObject gameObject2;
			if (transform == null)
			{
				gameObject2 = new GameObject("Settings");
				gameObject2.transform.parent = gameObject.transform;
			}
			else
			{
				gameObject2 = transform.gameObject;
			}
			return gameObject2;
		}

		public static GameObject HookupRecorder()
		{
			GameObject recordingSessionsRoot = GetRecordingSessionsRoot(createIfAbsent: true);
			GameObject gameObject = new GameObject();
			gameObject.transform.parent = recordingSessionsRoot.transform;
			return gameObject;
		}

		public static GameObject FindRecorder(RecorderSettings settings)
		{
			GameObject recordingSessionsRoot = GetRecordingSessionsRoot(createIfAbsent: false);
			if (recordingSessionsRoot == null)
			{
				return null;
			}
			for (int i = 0; i < recordingSessionsRoot.transform.childCount; i++)
			{
				Transform child = recordingSessionsRoot.transform.GetChild(i);
				RecorderComponent component = child.GetComponent<RecorderComponent>();
				if (component != null && component.session != null && component.session.settings == settings)
				{
					return component.gameObject;
				}
			}
			return null;
		}

		public static void RegisterInputSettingObj(string assetId, RecorderInputSetting input)
		{
			InputSettingsComponent inputsComponent = GetInputsComponent(assetId);
			inputsComponent.m_Settings.Add(input);
		}

		public static void UnregisterInputSettingObj(string assetId, RecorderInputSetting input)
		{
			InputSettingsComponent inputsComponent = GetInputsComponent(assetId);
			inputsComponent.m_Settings.Remove(input);
			UnityHelpers.Destroy(input);
		}

		public static InputSettingsComponent GetInputsComponent(string assetId)
		{
			GameObject settingsRoot = GetSettingsRoot(createIfAbsent: true);
			Transform transform = settingsRoot.transform.Find(assetId);
			if (transform == null)
			{
				transform = new GameObject().transform;
				transform.name = assetId;
				transform.parent = settingsRoot.transform;
			}
			InputSettingsComponent inputSettingsComponent = transform.GetComponent<InputSettingsComponent>();
			if (inputSettingsComponent == null)
			{
				inputSettingsComponent = transform.gameObject.AddComponent<InputSettingsComponent>();
				inputSettingsComponent.m_Settings = new List<RecorderInputSetting>();
			}
			return inputSettingsComponent;
		}
	}
}
